home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June (Extra) / CHIP 2006-06.3.iso / program / opensource / Inkscape-0.43-2.win32.exe / share / extensions / embedimage.py < prev    next >
Encoding:
Python Source  |  2005-11-06  |  1.5 KB  |  48 lines

  1. #!/usr/bin/env python 
  2. import inkex, base64
  3.  
  4. #a dictionary of all of the xmlns prefixes in a standard inkscape doc
  5. NSS = {u'sodipodi':u'http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd',
  6. u'cc':u'http://web.resource.org/cc/',
  7. u'svg':u'http://www.w3.org/2000/svg',
  8. u'dc':u'http://purl.org/dc/elements/1.1/',
  9. u'rdf':u'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  10. u'inkscape':u'http://www.inkscape.org/namespaces/inkscape',
  11. u'xlink':u'http://www.w3.org/1999/xlink'}
  12.  
  13. class MyEffect(inkex.Effect):
  14.     def __init__(self):
  15.         inkex.Effect.__init__(self)
  16.  
  17.     def effect(self):
  18.         ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=NSS)
  19.         
  20.         # if there is a selection only embed selected images
  21.         # otherwise embed all images
  22.         if (self.options.ids):
  23.             for id, node in self.selected.iteritems():
  24.                 if node.tagName == 'image':
  25.                     self.embedImage(node)
  26.         else:
  27.             path = '//image'
  28.             for node in inkex.xml.xpath.Evaluate(path,self.document, context=ctx):
  29.                 self.embedImage(node)
  30.     def embedImage(self, node):
  31.         xlink = node.attributes.getNamedItemNS(NSS[u'xlink'],'href')
  32.         if (xlink.value[:4]!='data'):
  33.             absref=node.attributes.getNamedItemNS(NSS[u'sodipodi'],'absref')
  34.             file = open(absref.value,"rb").read()
  35.             embed=True
  36.             if (file[:4]=='\x89PNG'):
  37.                 type='image/png'
  38.             elif (file[:2]=='\xff\xd8'):
  39.                 type='image/jpg'
  40.             else:
  41.                 embed=False
  42.             if (embed):
  43.                 xlink.value = 'data:%s;base64,%s' % (type, base64.encodestring(file))
  44.                 node.removeAttributeNS(NSS[u'sodipodi'],'absref')
  45.         
  46. e = MyEffect()
  47. e.affect()
  48.